package com.houger.ddd.seckill.facade.impl;

import com.houger.ddd.common.utils.BusinessException;
import com.houger.ddd.common.utils.R;
import com.houger.ddd.common.utils.StatusCode;
import com.houger.ddd.datamanager.entity.InventoryId;
import com.houger.ddd.datamanager.operator.InventoryOperator;
import com.houger.ddd.datamanager.utils.OperatorResult;
import com.houger.ddd.redpacket.facade.dto.UserInfoDto;
import com.houger.ddd.seckill.controller.SeckillController;
import com.houger.ddd.seckill.facade.SeckillFacede;
import com.houger.ddd.seckill.infra.datamanager.ActivityRewardItemDataManager;
import com.houger.ddd.seckill.infra.datamanager.ActivityRewardItemInventoryOperator;
import com.houger.ddd.seckill.infra.entity.ActivityRewardItem;
import com.houger.ddd.seckill.template.ActivityTemplate;
import com.houger.ddd.seckill.template.SecKillTemplate;
import com.houger.ddd.seckill.template.context.ActivityInstanceContext;
import com.houger.ddd.seckill.template.holder.BeanHolder;
import com.wf.captcha.SpecCaptcha;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;


/**
 * @author Houger.
 * @date 2024年05月31日 16:42.
 */
@Service
public class SeckillFacedeImpl implements SeckillFacede {

    @Resource
    private BeanHolder beanHolder;
    @Resource
    private ActivityRewardItemInventoryOperator activityRewardItemInventoryOperator;
    @Resource
    private ActivityRewardItemDataManager activityRewardItemDataManager;

    @Resource
    private SecKillTemplate secKillTemplate;

    @Override
    public R<String> getSeckillUri(final Long templateId, final Long userId) {
        final ActivityTemplate template = beanHolder.getTemplate(templateId);
        template.queryActivityContext(templateId,null,null);
        template.start(UserInfoDto.builder().userId(String.valueOf(userId)).build());
        final ActivityInstanceContext activityInstanceContext = template.getActivityInstanceContext();
        return R.ok(SeckillController.SECKILL_URI.formatted(activityInstanceContext.getInstanceDto().getInstanceCode()));
    }

    @Override
    public R<String> captcha(final Long instanceId, final Long userId) {
        SpecCaptcha specCaptcha = secKillTemplate.captcha(instanceId,userId);
        // 将key和base64返回给前端
        return R.ok(specCaptcha.toBase64());
    }

    @Override
    public R<String> doSeckill(final Long instanceId, final Long userId, final Long itemId,final String captcha) {
        secKillTemplate.queryActivityContext(null,null,instanceId);
        secKillTemplate.queryActivityInstanceContext(instanceId);
        secKillTemplate.check(instanceId,userId,itemId,captcha);
        //扣库存
        final ActivityRewardItem rewardItem = activityRewardItemDataManager.queryById(itemId);
        final InventoryId inventoryId = activityRewardItemInventoryOperator.getInventoryId(itemId);
        final OperatorResult result = activityRewardItemInventoryOperator.deduct(inventoryId, rewardItem.getRewardCount());
        if(!result.isSuccess()){
            throw new BusinessException(StatusCode.INSUFFICIENT_STOCK);
        }
        //异步生成订单，前端间隔一秒调用查询订单接口查询订单状态，如超一分钟未成功，则提示下单失败，请重新下单，并回退库存及限购限制
        // 成功则跳转到支付页面；
        //订单超过15分钟未支付则取消订单，回退库存及限购限制
        return R.ok("秒杀成功");
    }
}
